package nirvana.core.utils.copy;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * BeanUtil
 * support list copy
 *
 * @author linkanyway
 * @date 2022/5/30 22:34
 */
public class BeanTools extends BeanUtils {


    /**
     * copy and convert list of bean
     *
     * @param sources source list
     * @param target  {@link Supplier<T>}
     * @param <S>     element type of source list
     * @param <T>     element type of target list
     * @return target list
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        List<T> list = new ArrayList<> (sources.size ());
        for (S source : sources) {
            T t = target.get ();
            copyProperties (source, t);
            list.add (t);
        }
        return list;
    }


}
